package com.browsesoft.htmlcomponent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.dbtools.DBTools;
import com.browsesoft.user.User;
import com.browsesoft.workflow.support.sortInterface;

public class PersonalAddrListModel extends HTMLDBTableMaintenanceModel {
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 得到用户id
		User userName = (User) request.getSession().getAttribute("loginUser");
		String userId = (String) userName.getID();
		// 得到查询条件
		String condition = this.getCondition();
		if (parentID == null) {
			parentID = request.getParameter("point");
		}
		// 保持原有SQL非条件部分不动
		String executeSQL = new String(sql);
		// 有必备条件
		if (parentID != null) {
			// 添加必备条件
			executeSQL += " where processid=" + parentID;
			// 补上用户输入的条件
			if (condition != null && !condition.equals("")) {
				executeSQL += " and " + condition;
				executeSQL += " and userid=" + userId;
			} else {
				// 把用户的id加到条件中
				executeSQL += " where userid=" + userId;
			}
		}
		// 没有必备条件
		else {
			// 添加用户输入的条件
			if (condition != null && !condition.equals("")) {
				executeSQL += " where " + condition;
				executeSQL += " and userid=" + userId;
			} else {
				// 把用户的id加到条件中
				executeSQL += " where userid=" + userId;
			}
		}
		// 得到结果
		datas = DBTools.executeQueryWithTableHead(executeSQL);
		String tt = request.getParameter(component.getName().concat(".sort"));
		if (tt != null) {
			String temp = tt.substring(tt.lastIndexOf(".") + 1);
			if (!((sortInterface) this).getOrderBy()) {
				((sortInterface) this).sort(temp);
				((sortInterface) this).setOrderBy();
			} else if (((sortInterface) this).getOrderBy()) {
				((sortInterface) this).reverseSort(temp);
				((sortInterface) this).setOrderBy();
			}
		}
	}
}